import CommonBaseSchema from '#common/base_schema'

export default class extends CommonBaseSchema {
  protected tableName = 'stock_in_details'

  async up() {
    this.schema.createTable(this.tableName, (table) => {
      table.increments()

      table.integer('fk_shop_id').notNullable().unsigned().references('shops.id').comment('店铺ID')

      table
        .integer('fk_stock_order_id')
        .notNullable()
        .unsigned()
        .references('stock_orders.id')
        .comment('订单ID')

      table
        .integer('fk_warehouse_id')
        .notNullable()
        .unsigned()
        .references('dicts.id')
        .comment('仓库ID')

      table.integer('fk_goods_id').notNullable().unsigned().references('goods.id').comment('商品ID')

      table
        .integer('fk_supplier_id')
        .nullable()
        .unsigned()
        .references('dicts.id')
        .comment('供应商ID')

      table
        .integer('fk_buy_unit_id')
        .notNullable()
        .unsigned()
        .references('dicts.id')
        .comment('进货单位ID')

      table
        .integer('fk_standard_unit_id')
        .notNullable()
        .unsigned()
        .references('dicts.id')
        .comment('标准单位ID')

      table.integer('buy_unit_number').unsigned().defaultTo(0).comment('进货单位数量')
      table.integer('buy_unit_quantity').unsigned().defaultTo(0).comment('购买数量')
      table.integer('standard_unit_quantity').unsigned().defaultTo(0).comment('标准单位数量')
      table.integer('quantity').unsigned().defaultTo(0).comment('转换标准数量')
      table.integer('unit_price').unsigned().defaultTo(0).comment('单位单价')
      table.integer('total_amount').defaultTo(0).comment('总金额')
      table.tinyint('status').unsigned().defaultTo(1).comment('状态')
      table.string('memo', 255).defaultTo('').comment('备注')

      this.defineCommonColumns(table)
      table.comment('库存入库明细表')
    })
  }

  async down() {
    this.schema.dropTable(this.tableName)
  }
}
